using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using DNAide.Core;

namespace DNAide.Website.Templates.WebForms
{
	public partial class Default : BaseWebForm
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string contentXmlFile = Server.MapPath(string.Format("~/Content/{0}.xml", SiteMap.CurrentNode.Key));
			if (File.Exists(contentXmlFile))
			{
				using (XmlTextReader reader = new XmlTextReader(contentXmlFile))
				{
					while (reader.Read())
					{	
						if (reader.NodeType == XmlNodeType.Element)
						{
							switch (reader.Name){
								case "mainContent":
									this.MainContentHtml.Text = reader.ReadElementContentAsString();
									break;
								case "secondaryContent":
									this.SecondaryContentHtml.Text = reader.ReadElementContentAsString();
									break;
							}
						}
					}
				}
			}

			// Load in usercontrols
			LoadUserControls("mainContentUcs", this.MainContentUserControls);
			LoadUserControls("secondaryContentUCs", this.SecondartContentUserControls);
		}


		private void LoadUserControls(string attributeName, PlaceHolder placeholder)
		{
			string[] temp;
			string userControlList = CastAide.AsString(SiteMap.CurrentNode[attributeName]);
			if (!string.IsNullOrEmpty(userControlList))
			{
				temp = userControlList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string uc in temp)
				{
					placeholder.Controls.Add(this.LoadControl(string.Format("~/UserControls/{0}.ascx", uc)));
				}
			}
		}

		
	}
}
